package com.lc.demo.service.impl;

import com.lc.demo.bean.Course;
import com.lc.demo.bean.Signin;
import com.lc.demo.bean.Student;
import com.lc.demo.mapper.CourseMapper;
import com.lc.demo.mapper.SigninMapper;
import com.lc.demo.service.SigninService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SigninServiceImpl implements SigninService {

    @Autowired
    SigninMapper signinMapper;

    @Autowired
    CourseMapper courseMapper;

    //获取前一天日期
    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //    Date day = sdf.parse("2017-01-01");
    //
    //    long ms = day.getTime() - 1*24*3600*1000L;
    //    Date prevDay = new Date(ms);
    //    System.out.println(sdf.format(prevDay));


    @Override
    public void createWeekSignin() {

        //获取当前日期7天
        getWeekdayDate(new Date());
        //获取天数
        List<Date> dates = dateToWeek(new Date());
        Course course = new Course();
        for (int i = 1; i <= dates.size(); i++) {
            //根据i查询出星期几的课程数
            course.setWeekday(i);
            List<Course> courseList = courseMapper.select(course);
            //查询当前天数的所有课程
            for (Course course1 : courseList) {
                //根据课程查询出关联的班级下所有的学生
                List<Student> studentList = courseMapper.selectCourseStudentList(course1.getId());
                for (Student student : studentList) {
                    Signin signin = new Signin();
                    signin.setState(0);
                    signin.setStudentid(student.getUsername());
                    signin.setWeekday(i);
                    signin.setCoursename(course1.getName());
                    signin.setDaytime(dates.get(i - 1));
                    signin.setCourseid(course1.getId());
                    signinMapper.insert(signin);
                }
            }
        }
    }

    @Override
    public void deleteWeekSignin() throws ParseException {
        //获取当前日期7天
        getWeekdayDate(new Date());
        //获取天数
        List<Date> dates = dateToWeek(new Date());
        Course course = new Course();
        for (int i = 1; i <= dates.size(); i++) {
            //根据i查询出星期几的课程数
            course.setWeekday(i);
            List<Course> courseList = courseMapper.select(course);
            //查询当前天数的所有课程
            for (Course course1 : courseList) {
                //根据课程查询出关联的班级下所有的学生
                List<Student> studentList = courseMapper.selectCourseStudentList(course1.getId());
                for (Student student : studentList) {
                    Signin signin = new Signin();
                    signin.setStudentid(student.getUsername());
                    signin.setWeekday(i);
                    signin.setCoursename(course1.getName());
                    signin.setCourseid(course1.getId());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    //设置为东八区
                    String format = sdf.format(dates.get(i - 1));
                    //将字符串转化为时间
                    SimpleDateFormat sdfAsia = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = sdfAsia.parse(format);
                    signin.setDaytime(date);
                    signinMapper.delete(signin);
                }
            }
        }
    }

    @Override
    public List<Signin> getAllSignin() {
        return signinMapper.getAllSignin();

    }

    @Override
    public List<Map<String, Object>> getRanking() {
        DecimalFormat df = new DecimalFormat("#.00");

        //查询出 所有院系的签到率  排序好的
        List<Map<String, Object>> departmentList = signinMapper.selectDepartmentList();

        for (Map<String, Object> departmentMap : departmentList) {

            //根据 院系Id 查询出 排序好得 班级排名
            Integer departmentid = (Integer) departmentMap.get("id");
            //处理数据
            if (null == departmentMap.get("signcount")) {
                departmentMap.put("signcount", 0);
                departmentMap.put("percent", "0%");
            } else {
                BigDecimal departmentPercent = (BigDecimal) departmentMap.get("percent");
                departmentMap.put("percent", departmentPercent.movePointRight(2).setScale(2, RoundingMode.HALF_UP) + "%");
            }

            List<Map<String, Object>> classList = signinMapper.selectClassList(departmentid);
            //根据 班级  id 查询出  排好序的学生排名
            for (Map<String, Object> classMap : classList) {
                //处理数据
                if (null == classMap.get("signcount")) {
                    classMap.put("signcount", 0);
                    classMap.put("percent", "0%");
                } else {
                    BigDecimal classPercent = (BigDecimal) classMap.get("percent");
                    classMap.put("percent", classPercent.movePointRight(2).setScale(2, RoundingMode.HALF_UP) + "%");
                }
                Integer classId = (Integer) classMap.get("id");
                List<Map<String, Object>> studentList = signinMapper.selectStudentList(classId);
                //处理数据
                for (Map<String, Object> studentMap : studentList) {
                    if (null == studentMap.get("signcount")) {
                        studentMap.put("signcount", 0);
                        studentMap.put("percent", "0%");
                    } else {
                        BigDecimal studentPercent = (BigDecimal) studentMap.get("percent");
                        studentMap.put("percent", studentPercent.movePointRight(2).setScale(2, RoundingMode.HALF_UP) + "%");
                    }
                }
                classMap.put("children", studentList);
            }
            departmentMap.put("children", classList);
        }
        return departmentList;
    }

    @Override
    public Map<String, Object> getKeyMonitoringStudent() {
        Map<String, Object> map = signinMapper.getKeyMonitoringStudent();
        return map;
    }

    @Override
    public List<Signin> getStudentSign(Student student) {
        return signinMapper.getStudentSign(student.getUsername());

    }

    /**
     * 获取当前日期是星期几
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public Integer getWeekdayDate(Date dt) {
        int[] weekDays = {1, 2, 3, 4, 5, 6, 7};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w] - 1 == 0 ? 7 : weekDays[w] - 1;
    }


    /**
     * 根据日期获得所在周的日期
     *
     * @param mdate
     * @return
     */
    public List<Date> dateToWeek(Date mdate) {
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a - 1, fdate);
        }
        return list;
    }
}
